#include "Page.h"

Page::Page(int size, BufferManager * manager)
{
	pageSize = size;
	bm = manager;

	buffer = new char[pageSize];

	isInMem = true;
}

Page::~Page()
{
	if(isInMem)
		delete [] buffer;
}

bool Page::LoadFromFile()
{
	if(isInMem)
		return true;

	if(!buffer)
		buffer = new char[pageSize];
	else
	{
		cout << "loading error" << endl;
		return false;
	}

	if(!bm->CopyFromDisk(pageId, buffer))
		cout << "reading error" << endl;

	bm->LoadUpdate(pageId);

	isInMem = true;
	return true;
}

bool Page::SaveToFile()
{
	if(!isInMem)
		return false;

	if(bm->isDirty(pageId))
	{
		bm->SaveToDisk(pageId, buffer);
	}

	isInMem = false;
	bm->SaveUpdate(pageId);

	delete [] buffer;
	buffer = NULL;

	return true;
}
